import { defineAsyncComponent, reactive, markRaw } from 'vue'

interface IAsyncCpn {
  name: string
  cpn: any
}
export function useAsyncCpn() {
  const cpns: IAsyncCpn[] = reactive([])
  // glob支持绝对路径与相对路径导入
  const allDynamicCpns: any = import.meta.glob('/src/views/**/**.vue')

  // 排除掉home、login文件夹下
  const reg = new RegExp('home|login|iframe')
  for (const cpn in allDynamicCpns) {
    if (!reg.test(cpn)) {
      // ../../../../views/home/cpns/start.vue 截取组件名称
      const name = (cpn.split('/').pop()?.split('.')[0] as string)
      // 注册为异步组件
      cpns.push({
        name: name,
        cpn: markRaw(defineAsyncComponent(allDynamicCpns[cpn]))
      })
    }
  }
  // console.log(cpns)
  return { cpns }
}
